<?php
class Game extends CI_Model
{
	  function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		$this->load->database();
    }
	
	function getAll($row,$start)
	{
		$query=$this->db->query("SELECT * FROM game  ORDER BY stt DESC LIMIT $start , $row" );
	    return $query->result_array();
		}
	function theloai()
	{
	$query=$this->db->query('SELECT * FROM theloai');
	return $query->result_array();
	}		
	function getNew($row,$start)
	{
		$query=$this->db->query("SELECT * FROM game WHERE status=0 AND hide=0  ORDER BY stt DESC LIMIT $start , $row" );
	    return $query->result_array();
		}	
	function list_theloai()
	{
		$query=$this->db->query("SELECT tenloai,theloai.maloai, COUNT( game ) as tong 
FROM  `theloai` 
INNER JOIN game ON theloai.maloai = game.maloai
WHERE hide=0
GROUP BY theloai.maloai
LIMIT 0 , 30");
		return $query->result_array();
		
		}
	function insert($data)
	{
		$query=$this->db->query("INSERT INTO game(game,noidung,image,maloai,link,trangthai) VALUES ('".$data['tieude']."','".$data['noidung']."','".$data['anh']."','".$data['madanhmuc']."','".$data['link']."',1) ");
		
		}
	function countAll()
	{
		$this->db->select('*');
		$this->db->from('game');
		
		return $query=$this->db->count_all_results();
		
		}
	function countAll_id($id)
	{
		$this->db->select('*');
		$this->db->from('game');
		$this->db->where('maloai',$id);
		return $query=$this->db->count_all_results();
		
		}
	function countAll_haynhat()
	{
		$this->db->select('*');
		$this->db->from('game');
		$this->db->where('trangthai',2);
		$this->db->where('hide',0);
		return $query=$this->db->count_all_results();
		
		}
	function countAll_moinhat()
	{
		$this->db->select('*');
		$this->db->from('game');
		$this->db->where('status',0);
		$this->db->where('hide',0);
		return $query=$this->db->count_all_results();
		
		}			
	function filter($row,$start,$id)
	{
		$query=$this->db->query("SELECT * FROM game WHERE maloai=$id AND hide=0 ORDER BY stt DESC LIMIT $start , $row" );
	    return $query->result_array();
		
		}
		function haynhat($row,$start)
	{
		$query=$this->db->query("SELECT * FROM game WHERE trangthai=2 AND hide=0 ORDER BY stt DESC LIMIT $start , $row" );
	    return $query->result_array();
		
		}	
	function detail($id)
	{
		$query=$this->db->query("SELECT * FROM game WHERE stt=$id " );
	    return $query->row();
		}
	function update($data)
	{
		if($data['size']==0)
		{
			$query=$this->db->query("UPDATE game SET game='".$data['game']."', noidung='".$data['noidung']."',trangthai='".$data['trangthai']."',maloai='".$data['madanhmuc']."',link='".$data['link']."',status='".$data['status']."',hide='".$data['hide']."' WHERE stt='".$data['magame']."'");
			
			}
		else
		{$query=$this->db->query("UPDATE game SET game='".$data['game']."', noidung='".$data['noidung']."',trangthai='".$data['trangthai']."', image='".$data['anh']."',maloai='".$data['madanhmuc']."',link='".$data['link']."' ,status='".$data['status']."',hide='".$data['hide']."' WHERE stt='".$data['magame']."'");
			
			}
		}					
			
	function hide($id)
	{
		$query=$this->db->query("UPDATE game SET hide=1 WHERE stt='".$id."'");
		}
	function un_hide($id)
	{
		$query=$this->db->query("UPDATE game SET hide=0 WHERE stt='".$id."'");
		}	
		
	function link($id)
	{
		$query=$this->db->query("UPDATE game SET count=count+1 WHERE stt='".$id."'");
		}
	function maxdown($row,$start)
	{$query=$this->db->query("SELECT * FROM game WHERE hide=0 ORDER BY count DESC LIMIT $start , $row" );
		return $query->result_array();
		}
	//Kiêm tra login	
	function check_login($data)
		{
			$this->db->where('acc',$data['username']);
			$this->db->where('pass',$data['pass']);
			//$this->db->where('stt_domain',$data['stt_domain']);
			$q=$this->db->get('account');
			
			return $q->num_rows();
			}
		//Lưu id vào session
	function session($data)
		{
			$acc=array(
			'user'=>$data['username'],
			//'domain'=>$data['stt_domain']
			'validated'=>true
			);
			$this->session->set_userdata($acc);
			return $this->session->set_userdata($acc);
			}
		//Kiểm tra pass		
	function passcheck($data)
	{
		$this->db->where('acc',$data['user']);
		$this->db->where('pass',$data['pass']);
		$q=$this->db->get('account');
		return $q->num_rows();
		}	
//Hàm thay đổi password
    function changepass($data)
	{
		$value=array(
		'pass'=>$data['newpass']
		);
		$this->db->where('acc',$data['acc']);
		$this->db->update('account',$value);
		
		
		} 
								
	
	}
?>